//
//  SPNavigationController.m
//  SmartCity
//
//  Created by LSP on 2017/9/17.
//  Copyright © 2017年 NRH. All rights reserved.
//

#import "SPNavigationController.h"
#import "SPNavigationBar.h"
//#import <FDFullscreenPopGesture/UINavigationController+FDFullscreenPopGesture.h>

@interface UINavigationController ()
- (void)fd_pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
@end



@interface SPNavigationController () // : UINavigationController <UINavigationControllerDelegate>
@property (nonatomic, assign) BOOL inPushing;
@property (nonatomic, weak) id<UINavigationControllerDelegate> sp_delegate;
@end

@implementation SPNavigationController {
    BOOL _sp_viewLoaded;
}

- (void)setDelegate:(id<UINavigationControllerDelegate>)delegate {
    _sp_delegate = delegate;
}

- (id<UINavigationControllerDelegate>)delegate {
    if (_sp_viewLoaded) { // 该死的运行时（JZExtension），让我不得不加这个判断
        return self;
    }
    return nil;
}

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {
    if (self = [super initWithNavigationBarClass:[SPNavigationBar class] toolbarClass:nil]) {
        __weak typeof(self) weakSelf = self;
        [super pushViewController:rootViewController animated:NO];
        self.modalPresentationStyle = UIModalPresentationFullScreen;
        self.interactivePopGestureRecognizer.delegate = weakSelf;
    }
        return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _sp_viewLoaded = YES;
    [super setDelegate:self];
}

//- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    if ( [self respondsToSelector:@selector(interactivePopGestureRecognizer)] && animated == YES ){
//        self.interactivePopGestureRecognizer.enabled = NO;
//    }
//    SEL action = NSSelectorFromString(@"fd_pushViewController:animated:");
//    if (action && [self respondsToSelector:action]) {
//        [super fd_pushViewController:viewController animated:animated];
//    }else{
//        [super pushViewController:viewController animated:animated];
//    }
//    // TODO: 导入FDFullscreenPopGesture则需要使用以下方法。
////    [super fd_pushViewController:viewController animated:animated];
//}

//- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    如果当前正在做转场动画, 就不会立即推出控制器, 而是等转场完成后再调一次本方法.
//    故此重载在有转场动画未完成时无法正常执行
//    if (_inPushing)
//    return;
//
//    _inPushing = YES;
//    [super pushViewController:viewController animated:animated];
//}

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated

{
    
    if ( [self respondsToSelector:@selector(interactivePopGestureRecognizer)]&& animated == YES ){
        
        self.interactivePopGestureRecognizer.enabled = NO;
        
    }
    
    return [super popToRootViewControllerAnimated:animated];
    
}

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if( [self respondsToSelector:@selector(interactivePopGestureRecognizer)] ){
        
        self.interactivePopGestureRecognizer.enabled = NO;
        
    }
    
    return [super popToViewController:viewController animated:animated];
    
}


- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated {
    _inPushing = NO;
    if ([_sp_delegate respondsToSelector:@selector(navigationController:didShowViewController:animated:)]) {
        [_sp_delegate navigationController:navigationController didShowViewController:viewController animated:animated];
    }
    
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]){
        
        if (navigationController.childViewControllers.count == 1) {
            
            self.interactivePopGestureRecognizer.enabled = NO;
            
        }else
            
            self.interactivePopGestureRecognizer.enabled = YES;
        
    }
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if ([_sp_delegate respondsToSelector:@selector(navigationController:willShowViewController:animated:)]) {
        [_sp_delegate navigationController:navigationController willShowViewController:viewController animated:animated];
    }
}

- (UIInterfaceOrientationMask)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController {
    if ([_sp_delegate respondsToSelector:@selector(navigationControllerSupportedInterfaceOrientations:)]) {
        return [_sp_delegate navigationControllerSupportedInterfaceOrientations:navigationController];
    }
    return self.supportedInterfaceOrientations;
}
- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController {
    if ([_sp_delegate respondsToSelector:@selector(navigationControllerPreferredInterfaceOrientationForPresentation:)]) {
        return [_sp_delegate navigationControllerPreferredInterfaceOrientationForPresentation:navigationController];
    }
    return self.preferredInterfaceOrientationForPresentation;
}

- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
                                   interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController {
    if ([_sp_delegate respondsToSelector:@selector(navigationController:interactionControllerForAnimationController:)]) {
        return [_sp_delegate navigationController:navigationController interactionControllerForAnimationController:animationController];
    }
    return nil;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                            animationControllerForOperation:(UINavigationControllerOperation)operation
                                                         fromViewController:(UIViewController *)fromVC
                                                           toViewController:(UIViewController *)toVC {
    if ([_sp_delegate respondsToSelector:@selector(navigationController:animationControllerForOperation:fromViewController:toViewController:)]) {
        return [_sp_delegate navigationController:navigationController animationControllerForOperation:operation fromViewController:fromVC toViewController:toVC];
    }
    return nil;
}

- (void)pushViewController:(UIViewController *)viewController {
    if (_inPushing)
        return;
    
    _inPushing = YES;
    [self pushViewController:viewController animated:YES];
}

@end
